#!/bin/sh

# the nipapd package is not unpacked at this stage, so this script needs to
# know about the current version of the database
CURRENT_DB_VERSION=7

# to debug / develop
# export DEBCONF_DEBUG=developer
# export DEBIAN_PRIORITY=low
#
# to empty all answered questions from debconf db:
# if the script is run standalone, stuff ends up in package "unknown"
#   echo PURGE | debconf-communicate unknown
# if it's properly run from dpkg-configure or so, it should up under the package name
#   echo PURGE | debconf-communicate nipapd

# Source debconf library.
. /usr/share/debconf/confmodule

POSTGRES_INSTALLED=0
POSTGRES_RUNNING=0
NIPAP_DB_EXISTS=0
NIPAP_DB_VERSION=0

# determine if postgres is installed
su postgres -c 'psql --version' > /dev/null 2>&1
if [ $? -eq 0 ]; then
	POSTGRES_INSTALLED=1

	su postgres -c "psql -c '\l+'" >/dev/null 2>&1
	if [ $? -eq 0 ]; then
		POSTGRES_RUNNING=1
		# does the NIPAP database exist?
		if [ "`su postgres -c "psql -c '\l+'" | awk -F"|" '{ if ($1~/^ *nipap *$/) { gsub(/^ */, "", $1); gsub(/ *$/, "", $1); print $1 } }'`" = "nipap" ]; then
			NIPAP_DB_EXISTS=1

			# figure out version of db
			DB_COMMENT=`su postgres -c "psql -c '\l+'" | awk -F"|" '{ if ($1~/^ *nipap *$/) { print $9 } }'`
			echo "$DB_COMMENT" | grep "schema version: [0-9]\+$" > /dev/null 2>&1
			if [ $? -eq 0 ]; then
				# seems to be a DB comment with proper version
				NIPAP_DB_VERSION=`echo $DB_COMMENT | sed 's/.*schema version: //'`
			fi
		fi
	fi
fi

# ask where the database is running
db_input high nipapd/database_host || true
db_go

# automatically install db schema?
db_get nipapd/database_host
if [ "$RET" = "localhost" ]; then
	# It would be really weird if PostgreSQL wasn't installed
	if [ $POSTGRES_INSTALLED -eq 0 ]; then
		db_input high nipapd/local_db_not_installed || true
		db_go
		exit
	fi

	# It is not uncommon that PostgreSQL has been stopped as part of package
	# upgrades and if nipapd is in the same batch (again rather likely) we wouldn't
	# see PostgreSQL running by the time this script is executed.
	if [ $POSTGRES_RUNNING -eq 0 ]; then
		db_input high nipapd/local_db_not_running || true
		db_go
		exit
	fi

	# does user want automatic conf of db?
	db_input high nipapd/local_db_autoconf || true
	db_go

	# does user want automatic db schema upgrades?
	db_input high nipapd/local_db_upgrade || true
	db_go
else
	# should we automatically install db schema?
	db_input high nipapd/remote_db_install || true
	db_go

	# should we automatically upgrade db schema?
	db_input high nipapd/remote_db_upgrade || true
	db_go
fi


# SQlite upgrade?
if [ -n "`which nipap-passwd`" ]; then
	nipap-passwd latest-version 2>&1 >/dev/null
	if [ $? -eq 2 ]; then
		# TODO: not good to do db_reset here, it means we cannot configure this
		#       package using debconf-communicate.. where can we put it instead?
		db_reset nipapd/sqlite_upgrade
		db_input high nipapd/sqlite_upgrade || true
		db_go
	fi
fi

# should nipapd be started on system boot?
db_input high nipapd/startup || true
db_go
